﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TSL.Web.UI;
using TSL.BLL;

namespace TSL.AjaxResult
{
    public class UserPblish:BasePage
    {
        [TSL.Utility.jQueryInvokeMethod()]
        public QuKuanResult setQuKuanResult(string money, string password)
        {
            decimal moneyDe = -1;
            try
            {
                moneyDe = Convert.ToDecimal(money);
            }
            catch
            {
                return new QuKuanResult(-1, "非法入侵,本次将会记录！");
            }

            if (AsUser == null)
            {
                return new QuKuanResult(-1, "您尚未登录！");
            }

            string BankNum = string.Empty;
            DateTime BeginTime = WebConfig.Q_begintime;
            DateTime EndTime = WebConfig.Q_endtime;

            BeginTime = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd") + " " + BeginTime.ToString("HH:mm:ss"));
            EndTime = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd") + " " + EndTime.ToString("HH:mm:ss"));

            DateTime NowTime = Convert.ToDateTime(DateTime.Now.ToLongTimeString());
            string BankPass = string.Empty;
            TSL.Model.sys_bank model = new TSL.Model.sys_bank();
            TSL.BLL.sys_bank dals = new TSL.BLL.sys_bank();
            model.username = AsUser.username;
            TSL.Model.SYS_users userInfo = new TSL.BLL.SYS_users().GetModelByName(model.username);
            model.back_money = moneyDe.ToString();
            BankPass = TSL.Common.PassWord.deBankPW(password);
            TSL.Model.SYS_users Today_BankNum = new TSL.BLL.SYS_users().Get_Users_BankNum(model.username);
            if (Today_BankNum == null)
            {
                BankNum = "0";
            }
            else
            {
                BankNum = Today_BankNum.BankNum;
            }

            if (model.back_money == "")
            {
                return new QuKuanResult(-1, "请输入取款金额");
            }

            if (BankPass == "")
            {
                return new QuKuanResult(-1, "请输入取款密码");
            }

            if (userInfo.BankPass != BankPass)
            {
                return new QuKuanResult(-1, "您输入的取款密码不正确");
            }

            if (Convert.ToDecimal(userInfo.uermoney) < Convert.ToDecimal(model.back_money))
            {
                return new QuKuanResult(-1, "您的账户金额不足您本次取款");
            }

            if (Convert.ToDecimal(model.back_money) < WebConfig.backmoney)
            {
                return new QuKuanResult(-1, "您的取款金额低于平台最低取款金额");
            }

            if ((NowTime < BeginTime) || (NowTime > EndTime))
            {
                return new QuKuanResult(-1, "取款时间已过");
            }

            if (Convert.ToInt32(WebConfig.backnum) <= Convert.ToInt32(BankNum))
            {
                return new QuKuanResult(-1, "您已经超过今天的取款次数，请联系管理员");
            }

            dals.Addback(model);
            return new QuKuanResult();
        }

        [TSL.Utility.jQueryInvokeMethod()]
        public QuKuanResult acceptResult(string ID)
        {
            int intID = -1;
            try
            {
                intID = int.Parse(ID);
            }
            catch
            {
                return new QuKuanResult(-1, "非法入侵,本次将会记录！");
            }

            new Banks().Accept(intID);

            return new QuKuanResult();
        }

        [TSL.Utility.jQueryInvokeMethod()]
        public setUserInfoResult setUserInfo(string chs_name, string Bank_name, string Bank_Bank, string Bank_Num, string QQ, string Email, string BankPass)
        {
            int Bank_NumInt = 0;
            try
            {
                Bank_NumInt = int.Parse(Bank_Bank);
            }
            catch
            {
                return new setUserInfoResult(-1, "非法入侵，本次将会记录！");
            }

            if (AsUser == null)
            {
                return new setUserInfoResult(-1, "用户未登陆！");
            }


            TSL.Model.SYS_users model = new TSL.Model.SYS_users();
            TSL.BLL.SYS_users dals = new TSL.BLL.SYS_users();
            model.id = AsUser.id;
            model.CHS_name = chs_name;
            model.bank_name = Bank_name;
            model.bank_bank = Bank_NumInt;
            model.bank_num = Bank_Num;
            model.qq = QQ;
            model.email = Email;
            model.BankPass = TSL.Common.PassWord.deBankPW(BankPass);

            if (string.IsNullOrEmpty(chs_name))
            {
                return new setUserInfoResult(-1, "用户姓名不能为空！");
            }

            if (Convert.ToInt32(model.bank_bank) == 0)
            {
                return new setUserInfoResult(-1, "请选择银行类型！");
            }

            if (model.bank_num == "")
            {
                return new setUserInfoResult(-1, "账号不能为空！");
            }

            if (model.bank_num.Length < 6)
            {
                return new setUserInfoResult(-1, "银行账户不能小于6位！");
            }

            if (model.bank_name == "")
            {
                return new setUserInfoResult(-1, "银行户名不能为空！");
            }

            if (model.bank_name.Length < 2)
            {
                return new setUserInfoResult(-1, "银行户名不能小于2位！");
            }

            //if (model.qq == "")
            //{
            //    return new setUserInfoResult(-1, "QQ号码不能为空！");
            //}

            //if (model.email == "")
            //{
            //    return new setUserInfoResult(-1, "Email不能为空！");
            //}

            if (model.BankPass == "")
            {
                return new setUserInfoResult(-1, "资金密码不能为空！");
            }

            dals.UpdateUser(model);
            return new setUserInfoResult();
        }

        [TSL.Utility.jQueryInvokeMethod()]
        public setUserInfoResult setUserPassWord(string OldPWD, string NewPWD)
        {
            if (AsUser == null)
            {
                return new setUserInfoResult(-1, "用户未登陆！");
            }

            if (string.IsNullOrEmpty(OldPWD))
            {
                return new setUserInfoResult(-1, "请输入旧密码！");
            }

            if (string.IsNullOrEmpty(NewPWD))
            {
                return new setUserInfoResult(-1, "请输入新密码！");
            }

            OldPWD = TSL.Common.PassWord.deUserPW(OldPWD);
            if (AsUser.userpass != OldPWD)
            {
                return new setUserInfoResult(-1, "您输入的旧密码不正确！");
            }

            TSL.BLL.SYS_users dals = new TSL.BLL.SYS_users();
            dals.Update_Pwd(AsUser.username, TSL.Common.PassWord.deUserPW(NewPWD));

            return new setUserInfoResult();
        }

        [TSL.Utility.jQueryInvokeMethod()]
        public setUserInfoResult setUserBankWord(string OldPWD, string NewPWD)
        {
            if (AsUser == null)
            {
                return new setUserInfoResult(-1, "用户未登陆！");
            }

            if (string.IsNullOrEmpty(OldPWD))
            {
                return new setUserInfoResult(-1, "请输入旧密码！");
            }

            if (string.IsNullOrEmpty(NewPWD))
            {
                return new setUserInfoResult(-1, "请输入新密码！");
            }

            OldPWD = TSL.Common.PassWord.deUserPW(OldPWD);
            if (AsUser.BankPass != OldPWD)
            {
                return new setUserInfoResult(-1, "您输入的旧密码不正确！");
            }

            TSL.BLL.SYS_users dals = new TSL.BLL.SYS_users();
            dals.Update_FundPwd(AsUser.username, TSL.Common.PassWord.deUserPW(NewPWD));

            return new setUserInfoResult();
        }
    }
}
